from asyncio.windows_events import NULL
from cmath import nan
import xml.etree.ElementTree as ET
import glob
import json
import os

from numpy import double
from config import path

def new_event():
    return {
        "id": 0,
        "group_id": 0,
        'limit': 0,
        "created_time": 0,
        "start_time":0,
        "organizers": list(),
        "yes": 0,
        "has_venue": False,
        "country": "",
        "city":"",
        # Magic number if data has no latitude attribute
        "latitude": 114115.,
        "longitude": 114115., 
        "member and response": dict()
    }
def new_member():
    return {
        "id": 0,
        "topics": list(),
        "joined_time": 0,
        "country": "",
        "city": "",
        "latitude": 114115.,
        "longitude": 114115.
    }
def new_group():
    return {
        "id":0,
        "topics":list(),
        "created_time":0,
        "organizer_id":0,
        "member_number":0,
        "country": "",
        "city": ""
    }
def new_topic(id = 0):
    return {
        'id': id,
        'groups': list(),
        'members': list()
    }

def read_pastevent(path):
    # record:eventhosts->memberid,group->id,yes_rsvp_count,created,time,revp_limit,id
    # recode:member and response(using dict)
    event_list = list()
    PassEvent_files = glob.glob(path+r"PastEvent*.xml")
    for single_file in PassEvent_files:
        # print(single_file)
        rsvp_file = single_file.replace("PastEvent", "RSVPs")
        if(not os.path.exists(rsvp_file)):
            continue
        temp_event = new_event()
        tree = ET.parse(single_file)
        root = tree.getroot()
        eventhosts = root.find("event_hosts")
        if(eventhosts!=None):
            for host_item in eventhosts.findall("event_hosts_item"):
                for host in host_item.findall("member_id"):
                    temp_event["organizers"].append(host.text)
        else:
            temp_event["organizers"].append('0')
        group = root.find("group")
        temp_event['group_id'] = group.find("id").text
        temp_event['yes'] = int(root.find("yes_rsvp_count").text)
        temp_event['created_time'] = root.find("created").text
        temp_event['start_time'] = root.find("time").text
        # Modified 2022/4/16
        venue = root.find("venue")
        if venue != None:
            temp_event['has_venue'] = True
            if venue.find("country")!= None:
                temp_event['country'] = venue.find("country").text
            if venue.find("city") != None:
                temp_event['city'] = venue.find("city").text
            if venue.find("lat") != None:
                temp_event['latitude'] = float(venue.find('lat').text)
            if venue.find("lon") != None:
                temp_event['longitude'] = float(venue.find('lon').text)
        else :
            temp_event['has_venue'] = False
        #
        if(root.find("rsvp_limit")!=None):
            temp_event['limit'] = root.find("rsvp_limit").text
        else:
            temp_event['limit'] = temp_event['yes']
        temp_event['id'] = root.find("id").text
        
        tree = ET.parse(rsvp_file)
        root = tree.getroot().find("items")
        items = root.findall("item")
        for single_item in items:
            temp_event['member and response'][single_item.find("member").find("member_id").text] = single_item.find("response").text
        
        
        
        event_list.append(temp_event)
    return event_list    

def read_member(path):
    # record: joined,id,country,city,topic
    member_list = list()
    member_files = glob.glob(path+r"Memeber*.xml")
    for single_file in member_files:
        print(single_file)
        temp_member = new_member()
        tree = ET.parse(single_file)
        root = tree.getroot()
        temp_member['joined_time'] = root.find("joined").text
        topic = root.find("topics").findall("topics_item")
        if(len(topic)==0):
            temp_member['topics'] = []
        else:
            temp_member['topics'] = [_.find("id").text for _ in topic]
        temp_member["id"] = root.find("id").text
        if(root.find("country")==None):
            temp_member["country"] = 'unknown'
        else:            
            temp_member["country"] = root.find("country").text
        if(root.find("city")==None):
            temp_member['city'] = 'unknown'
        else:
            temp_member['city'] = root.find("city").text

        # Modified 2022/4/16
        if (root.find('lat')!=None):
            temp_member['latitude'] = float(root.find('lat').text)
        if (root.find('lon')!=None):
            temp_member['longitude'] = float(root.find('lon').text) 
        member_list.append(temp_member)
    return member_list    

def read_group(path):
    # record:id,topics,created_time,organizer_id,member_number,country,city
    group_list = []
    group_files = glob.glob(path+r"Group*.xml")
    for single_file in group_files:
        print(single_file)
        temp_group = new_group()
        tree = ET.parse(single_file)
        root = tree.getroot()
        temp_group['created_time'] = root.find("created").text
        temp_group['id'] = root.find("id").text
        topic = root.find("topics").findall("topics_item")
        if(len(topic)==0):
            temp_group['topics'] = []
        else:
            temp_group['topics'] = [_.find("id").text for _ in topic]
        temp_group['organizer_id'] = root.find('organizer').find('member_id').text
        temp_group['member_number'] = int(root.find("members").text)
        if(root.find("country")==None):
            temp_group["country"] = 'unknown'
        else:            
            temp_group["country"] = root.find("country").text
        if(root.find("city")==None):
            temp_group['city'] = 'unknown'
        else:
            temp_group['city'] = root.find("city").text
        group_list.append(temp_group)
    return group_list
        
        
        
def read_data(path, rebuild=False):
    event_path = os.path.join(path, 'event_list.json')
    if os.path.exists(event_path) and rebuild==False:
        with open (event_path) as f:
            event_list = json.load(f)       
    else:
        event_list = read_pastevent(path)
        with open(event_path, 'w') as f:
            json.dump(event_list, f)
    print("load event info complete")
    
    member_path = os.path.join(path, 'member_list.json')     
    if os.path.exists(member_path) and rebuild==False:
        with open(member_path) as f:
            member_list = json.load(f)
    else:
        member_list = read_member(path)
        with open(member_path, "w") as f:
            json.dump(member_list, f)
    print("load member info complete")

    group_path = os.path.join(path, 'group_list.json')     
    if os.path.exists(group_path) and rebuild==False:
        with open(group_path) as f:
            group_list = json.load(f)
    else:
        group_list = read_group(path)
        with open(group_path, "w") as f:
            json.dump(group_list, f)
    print("load group info complete")

    topic_json_path = os.path.join(path, "topic_dict.json")
    topic_dict = dict()
    if os.path.exists(topic_json_path) and (not rebuild):
        with open(topic_json_path) as f:
            topic_dict = json.load(f)
    else:
        for member in member_list:
            for topic in member['topics']:
                if not (topic in topic_dict.keys()):
                    topic_dict[topic] = new_topic(topic)
                if not(member['id'] in topic_dict[topic]["members"]):
                    topic_dict[topic]["members"].append(member['id'])
        for group in group_list:
            for topic in group['topics']:
                if not (topic in topic_dict.keys()):
                    topic_dict[topic] = new_topic(topic)
                if not (group['id'] in topic_dict[topic]['groups']):
                    topic_dict[topic]["groups"].append(group['id'])
        with open(topic_json_path, 'w') as f:
            json.dump(topic_dict, f)
    print("load topic info complete")
    
    return event_list, topic_dict, member_list, group_list
    
def test(path):
    # test if each event have a RSVP file
    # don't need to use
    PassEvent_files = glob.glob(path+r"PastEvent*.xml")
    for single_file in PassEvent_files:
        rsvp_file = single_file.replace("PastEvent","RSVPs")
        if(os.path.exists(rsvp_file)):
            print("yes")
        else:
            print(rsvp_file)



if __name__ == "__main__":
    # read_data(path,rebuild=True)

    # Modified 2022/4/16
    # event_list = read_pastevent(path)
    # curr_path = "./data_process/All_Unpack/"
    # event_path = os.path.join(curr_path, "event_list.json")
    # with open(event_path, 'w') as f :
    #     json.dump(event_list, f)
    member_list = read_member(path)
    curr_path = "./data_process/All_Unpack/"
    member_path = os.path.join(curr_path, "member_list.json")
    with open(member_path, 'w') as f :
        json.dump(member_list, f)

    # test(path)